/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package SaleManager.Book.Logic;

import Data.DAO.IBookStore;
import Data.DAO.IDataStore;
import data.pojo.Book;

import java.util.ArrayList;
import java.util.List;
import java.util.Vector;

/**
 *
 * @author Kite
 */
public class AddBookManage {

    private ArrayList<Book> newList = new ArrayList<Book>();
    private IDataStore bookDS = new IBookStore();   
    private ArrayList<Book> allBooks;

    public AddBookManage() {
        allBooks = (ArrayList<Book>) bookDS.getObjects();
    }


    public Vector<List> searchTableVector(String key, String keywords) {
        return new Vector<List>(toBookTable(bookDS.searchObjectsByAttr(key, ".*" + keywords + ".*")));
    }

    public Vector<List> bookTableVector() {
        return new Vector<List>(toBookTable(newList));
    }

    public Vector<Vector<String>> toBookTable(List list) {
        try {
            Vector<Book> seList = new Vector(list);
            Vector<Vector<String>> reList = new Vector<Vector<String>>();
            for (int i = 0; i < seList.size(); i++) {
                reList.add((Vector<String>) toBookStringArray(seList.get(i)));
            }
            return reList;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    public List toBookStringArray(Book book) {
        Vector<String> list = new Vector<String>();
        list.add(book.getISBN());//ISBN
        list.add(book.getTitle());//title
        list.add(book.getAuthor());//Author
        list.add(((Double) book.getPrice()).toString());//price     
        list.add(String.valueOf(book.getPublisher()));//publicsher
        list.add("");//publicationDate
        return list;
    }

    public List<Book> getNewList() {
        return this.newList;
    }

    public boolean addToNewList(Book book) {
        if (this.isValid(book)) {
            this.newList.add(book);
            return true;
        } else {
            return false;
        }
    }

    private boolean isValid(Book book) {
        if (!this.bookDS.searchObjectsByAttr("ISBN", String.valueOf(book.getISBN())).isEmpty()) {
            return false;
        }
        for (Book b : newList) {
            if (b.getISBN() == book.getISBN()) {
                return false;
            }
        }
        return true;
    }

    public Book getByIndex(int index) {
        return this.newList.get(index);
    }

    public boolean setByIndex(int index, Book book) {
        if (this.isValid(book)) {
            this.newList.set(index, book);
            return true;
        } else {
            return false;
        }
    }

    public void removeFromNewList(int index) {
        this.newList.remove(index);
    }

    public boolean commitNewList() {
        if (this.newList.isEmpty()) {
            return false;
        } else {
            this.bookDS.add(newList);
            newList = new ArrayList<Book>();
            return true;
        }
    }
}
